import { login, getInfo, geButtonbyMenu } from '@/api/user'
import { getToken, setToken, removeToken, setUserInfo, removeUserInfo, removeStatus } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    buttonList: [],
    roles: []
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_BUTTONLIST: (state, buttonList) => {
    state.buttonList = buttonList
    // state.buttonList = ['users_add', 'users_status', 'users_edit', 'users_del', 'teams_add', 'teams_status', 'permissions_topath', 'teams_edit', 'teams_del', 'dataSource_add', 'dataSource_status', 'dataSource_deit', 'dataSource_del', 'timedTask_add', 'timedTask_status', 'timedTask_edit', 'timedTask_del', 'backupData_download', 'monTimedTask_monitoring', 'monTimedTask_edit', 'monTimedTask_del', 'monTimedTask_status']
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ loginName: username.trim(), passWord: password }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data)
        setToken(data)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response
        if (!data) {
          reject('Verification failed, please Login again.')
        }
        setUserInfo(data)
        const { sysAuths, realName } = data
        const permissionCode = JSON.stringify(sysAuths.map(res => (res.permissionCode)))

        let permissionSet = new Set()
        if (permissionCode.search('users') > 0) {
          permissionSet.add('users')
          permissionSet.add('systemConfig')
        }
        if (permissionCode.search('teams') > 0) {
          permissionSet.add('teams')
          permissionSet.add('systemConfig')
        }
        if (permissionCode.search('permissions') > 0) {
          permissionSet.add('permissions')
          permissionSet.add('systemConfig')
        }
        if (permissionCode.search('roles') > 0) {
          permissionSet.add('roles')
          permissionSet.add('systemConfig')
        }
        if (permissionCode.search('logs') > 0) {
          permissionSet.add('logs')
          permissionSet.add('systemConfig')
        }

        if (permissionCode.search('dataSource') > 0) {
          permissionSet.add('dataSource')
          permissionSet.add('baseConfig')
        }
        if (permissionCode.search('timedTask') > 0) {
          permissionSet.add('timedTask')
          permissionSet.add('baseConfig')
        }
        if (permissionCode.search('backupData') > 0) {
          permissionSet.add('backupData')
        }
        if (permissionCode.search('monTimedTask') > 0) {
          permissionSet.add('monTimedTask')
          permissionSet.add('msg')
        }
        if (permissionCode.search('msgList') > 0) {
          permissionSet.add('msgList')
          permissionSet.add('monitoring')
        }
        if (data.loginName === 'admin') {
          permissionSet = new Set(['admin'])
        }
        const permission = Array.from(permissionSet)
        commit('SET_ROLES', permission)
        commit('SET_NAME', realName)
        commit('SET_AVATAR', 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif')
        resolve(permission)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // geButtonbyMenu
  geButtonbyMenu({ commit }, code) {
    return new Promise((resolve, reject) => {
      geButtonbyMenu({ menuCode: code }).then(res => {
        commit('SET_BUTTONLIST', res.data)
        resolve(res)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      removeUserInfo()
      removeStatus()
      resetRouter()
      commit('RESET_STATE')
      resolve()
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      removeUserInfo()
      removeStatus()
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

